-- SignInM
-- Create by chengb Dec/12/2014
-- 签到功能

module("SignInM", package.seeall);

-- 偏移的时间
local offsetTime = 5 * 3600

-- 签到配置表
local signInCfg = {}

-- 签到奖励配置表
local bonusCfg = {}

-- 载入配置表
local _init = false;
local loadCsv = function()
    if _init then return end
    _init = true;

    -- 转换签到配置表
    local sign_in_CSV = get_sign_in_CSV();
    for i = 1, #sign_in_CSV do
        local days = sign_in_CSV[i].days;
        signInCfg[days] = sign_in_CSV[i];

        -- 转换奖励信息
        signInCfg[days].bonus = string.explode(sign_in_CSV[i].bonus, "|");
    end
    sign_in_CSV = {};
    
    -- 签到奖励配置表
    local sign_in_bonus_CSV = get_sign_in_bonus_CSV();
    for i = 1, #sign_in_bonus_CSV do
        -- 奖励信息
        local desc = string.explode(sign_in_bonus_CSV[i].bonus, "+");
        local bonus = {};
        local rule = sign_in_bonus_CSV[i].rule;
        local icon = sign_in_bonus_CSV[i].icon;

        for index, info in pairs(desc) do
            local arr = string.explode(string.sub(info, 3, -2), ",");
            local type;
            local classId;
            local amount;

            if string.startWith(info, "1(") then
                -- 这是物品奖励
                type = 1
                classId = tonumber(arr[1])
                amount  = tonumber(arr[2])
            else
                -- 这是属性
                type = 2;
                classId = arr[1]
                amount  = tonumber(arr[2])
            end

            -- 奖励信息
            bonus[index] = { type, classId, amount, icon}
        end

        bonusCfg[rule] = bonus;
    end
    
    sign_in_bonus_CSV = {};
end

-- 模块初始化
function init()
    loadCsv();
end

-- 获取本月签到了几次
function calcSignInTimes()
    local data = ME.user.signIn
    if not data or #data <= 0 then
        return 0
    end
    
    -- 第二个是签到数据
    local signInTime = data[1]
    -- 如果上次签到不是本月内的
    if not isSameMonth(signInTime - TimeM.getTimeOffsetDifference(), 
        TimeM.getServerTime() - TimeM.getTimeOffsetDifference(), offsetTime) then
        return 0
    end
    
    -- 签到天数
    return #table.keys(data[2])
end

-- 检查能否签到，只要判断今日是否签到过
function canSignIn()
    local data = ME.user.signIn
    if not data or #data <= 0 then
        -- 没有签到数据？不应该
        return false
    end
    
    -- 当前时间
    local curTime = TimeM.getServerTime() - TimeM.getTimeOffsetDifference();
    -- 最近一次签到时间
    local lastSignIn = data[1] - TimeM.getTimeOffsetDifference();
    
    -- 不应该比当前时间还晚
    if lastSignIn > curTime then
        return false
    end
    
    -- 不是同一个月的
    if not isSameMonth(lastSignIn, curTime, offsetTime) then
        return true
    end
    
    -- 不是同一天的
    if not isSameDay(lastSignIn, curTime, offsetTime) then
        return true
    end
    
    -- 判断今天是否签到过了
    local day = tonumber(os.date("%d", curTime - offsetTime));
    return data[2][day] ~= 1
end

-- 签到
function signIn()
    -- 检查没通过
    if true ~= canSignIn() then
        return false
    end
    
    -- 本月已经签到几次了
    local days = calcSignInTimes()
    
    -- 奖励
    local bonusInfo = getBonus(days + 1)
    local amount
    
    for _, arr in pairs(bonusInfo) do
        if arr[1] == 1 then
            -- 物品
            local amount = ItemM.getAmount(ME.user, arr[2]) + arr[3]
            ItemM.updateItem(ME.user, arr[2], amount)
        elseif arr[1] == 2 then
            -- 属性
            local amount = ME.user.dbase:query(arr[2], 0) + arr[3]
            ME.user.dbase:set(arr[2], amount)
        end
    end
    
    -- 签到数据
    local data = ME.user.signIn
    -- 如果和上次签到时间不是同一个月
    if not isSameMonth(data[1] - TimeM.getTimeOffsetDifference(), TimeM.getServerTime() - TimeM.getTimeOffsetDifference(), offsetTime) then
        -- 本月签到数据清空
        data[2] = {}
    end
    local day = tonumber(os.date("%d", TimeM.getServerTime() - TimeM.getTimeOffsetDifference() - offsetTime))
    
    -- 更新下签到数据
    data[1] = os.time()
    data[2][day] = 1
    
    ME.user.signIn = data
    
    -- 签到事件
    EventMgr.fire(event.SIGN_IN, days + 1);
    
    return true
end

-- 获取奖励，签到days天的奖励
function getBonus(days)
    local mon = tonumber(os.date("%m", os.time()))
    local config = signInCfg[days].bonus    -- 奖励配置
    local index  = math.min(mon, #config)   -- 后面的月份如果没有配置，那么就往前取最近一个月份的奖励
    local rule   = config[index]
    local multi  = FormulaM.invoke("CALC_SIGN_IN_BONUS_MULTI", days)    -- 奖励倍数
    local bonus  = bonusCfg[rule]   -- 基础奖励
    local ret = {}
    
    for _, arr in pairs(bonus) do
        table.insert(ret, { arr[1], arr[2], arr[3] * multi, arr[4] })
    end
    
    return ret
end

-- 是否有新提醒
function isRemind()
	return canSignIn();
end


